package com.elai.operate.controller;

import java.util.List;

import com.elai.operate.domain.OperateChannelConfig;
import com.elai.operate.service.IOperateChannelConfigService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.elai.common.annotation.Log;
import com.elai.common.core.controller.BaseController;
import com.elai.common.core.domain.AjaxResult;
import com.elai.common.enums.BusinessType;
import com.elai.operate.domain.OperateChannelTemplate;
import com.elai.operate.service.IOperateChannelTemplateService;
import com.elai.common.utils.poi.ExcelUtil;
import com.elai.common.core.page.TableDataInfo;

/**
 * 系统通道Controller
 * 
 * @author elai
 * @date 2021-10-18
 */
@RestController
@RequestMapping("/operate/channelTemplate")
public class OperateChannelTemplateController extends BaseController
{
    @Autowired
    private IOperateChannelConfigService operateChannelConfigService;
    @Autowired
    private IOperateChannelTemplateService operateChannelTemplateService;

    /**
     * 查询系统通道列表
     */
    @PreAuthorize("@ss.hasPermi('operate:channelTemplate:list')")
    @GetMapping("/list")
    public TableDataInfo list(OperateChannelTemplate operateChannelTemplate)
    {
        startPage();
        List<OperateChannelTemplate> list = operateChannelTemplateService.selectOperateChannelTemplateList(operateChannelTemplate);
        return getDataTable(list);
    }

    @GetMapping("/getTemplatelistByType")
    public AjaxResult getTemplatelistByType(OperateChannelTemplate operateChannelTemplate)
    {
        List<OperateChannelTemplate> list = operateChannelTemplateService.selectOperateChannelTemplateList(operateChannelTemplate);
        return AjaxResult.success(list);
    }

    /**
     * 导出系统通道列表
     */
    @PreAuthorize("@ss.hasPermi('operate:channelTemplate:export')")
    @Log(title = "系统通道", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(OperateChannelTemplate operateChannelTemplate)
    {
        List<OperateChannelTemplate> list = operateChannelTemplateService.selectOperateChannelTemplateList(operateChannelTemplate);
        ExcelUtil<OperateChannelTemplate> util = new ExcelUtil<OperateChannelTemplate>(OperateChannelTemplate.class);
        return util.exportExcel(list, "系统通道数据");
    }

    /**
     * 获取系统通道详细信息
     */
    @PreAuthorize("@ss.hasPermi('operate:channelTemplate:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return AjaxResult.success(operateChannelTemplateService.selectOperateChannelTemplateById(id));
    }

    /**
     * 新增系统通道
     */
    @PreAuthorize("@ss.hasPermi('operate:channelTemplate:add')")
    @Log(title = "系统通道", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OperateChannelTemplate operateChannelTemplate)
    {
        OperateChannelConfig config=operateChannelConfigService.selectOperateChannelConfigById(operateChannelTemplate.getChannelConfigId());
        operateChannelTemplate.setChannelServiceCode(config.getChannelServiceCode());
        operateChannelTemplate.setChannelServiceName(config.getChannelServiceName());
        OperateChannelTemplate param=new OperateChannelTemplate();
        param.setChannelConfigId(operateChannelTemplate.getChannelConfigId());
        List<OperateChannelTemplate> list=operateChannelTemplateService.selectOperateChannelTemplateList(param);
        if(list.size()>0){
            return AjaxResult.error(config.getChannelServiceName()+"已经存在，不能重复添加!");
        }
        return toAjax(operateChannelTemplateService.insertOperateChannelTemplate(operateChannelTemplate));
    }

    /**
     * 修改系统通道
     */
    @PreAuthorize("@ss.hasPermi('operate:channelTemplate:edit')")
    @Log(title = "系统通道", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OperateChannelTemplate operateChannelTemplate)
    {
        return toAjax(operateChannelTemplateService.updateOperateChannelTemplate(operateChannelTemplate));
    }

    /**
     * 删除系统通道
     */
    @PreAuthorize("@ss.hasPermi('operate:channelTemplate:remove')")
    @Log(title = "系统通道", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(operateChannelTemplateService.deleteOperateChannelTemplateByIds(ids));
    }
}
